package com.moli.iov.admin.service.impl;

import com.moli.iov.admin.model.vo.req.UserVo;
import com.moli.iov.admin.service.IUserFacadeService;
import com.moli.iov.base.model.dto.req.VehicleFollowGroupReqDto;
import com.moli.iov.base.remote.IVehicleFollowGroupRemoteService;
import com.moli.iov.base.service.IVehicleFollowGroupService;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.enums.UserTypeEnum;
import com.moli.iov.sys.model.dto.req.UserDto;
import com.moli.iov.sys.model.po.User;
import com.moli.iov.sys.remote.IAuthenticationRemoteService;
import com.moli.iov.sys.remote.IPermissionRemoteService;
import com.moli.iov.sys.remote.IUserRemoteService;
import com.moli.iov.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 用户管理
 * </p>
 *
 * @author zql
 * @since 2019/9/9 14:55
 */
@Slf4j
@Service
public class UserFacadeServiceImpl implements IUserFacadeService {

  @Reference(group = "${service.group}", check = false)
  private IUserRemoteService userRemoteService;

  @Reference(group = "${service.group}", check = false)
  private IPermissionRemoteService permissionRemoteService;

  @Reference(group = "${service.group}", check = false)
  private IAuthenticationRemoteService authenticationRemoteService;

  @Reference(group = "${service.group}", check = false)
  private IVehicleFollowGroupRemoteService vehicleFollowGroupRemoteService;

  /**
   * 分页查询用户列表
   * @param userVo
   * @return
   */
  @Override
  public RestResponse<PageExtend<User>> listUserOfPage(UserVo userVo) {
    UserDto userDto = convert(userVo);
    if(UserTypeEnum.COMPANY.getFlag() == userVo.getSysUserType()){
      userDto.setCompanyId(userVo.getSysCompanyId());
    }
    return userRemoteService.listUserOfPage(userDto);
  }

  /**
   * 添加用户
   * @param userVo
   * @return
   */
  @Override
  public RestResponse saveUser(UserVo userVo) {
    UserDto userDto = convert(userVo);
    RestResponse res = userRemoteService.saveUser(userDto);
    if(res!=null && res.flag){
      RestResponse<User> user = userRemoteService.getUserByAccount(userVo.getEmail());
      Long userId = user.getData().getUserId();
      VehicleFollowGroupReqDto dto = new VehicleFollowGroupReqDto();
      dto.setCompanyId(userVo.getCompanyId());
      dto.setCompanyName(userVo.getCompanyName());
      dto.setUserId(userId);
      Date gmt = new Date();
      dto.setGmtCreate(gmt);
      dto.setGmtModified(gmt);
      dto.setIsDelete(0);
      dto.setGroupName("默认关注组");
      vehicleFollowGroupRemoteService.save(dto);
    }
    return res;
  }

  /**
   * 修改用户
   * @param userVo
   * @return
   */
  @Override
  public RestResponse updateUser(UserVo userVo) {
    return userRemoteService.updateUser(convert(userVo));
  }

  /**
   * 重置密码
   * @param userVo
   * @return
   */
  @Override
  public RestResponse resetPwd(UserVo userVo) {
    return userRemoteService.resetPwd(convert(userVo));
  }

  /**
   * 修改密码
   * @param userVo
   * @return
   */
  @Override
  public RestResponse updatePwd(UserVo userVo) {

    UserDto userDto = convert(userVo);
    userDto.setUserId(userVo.getSysUserId());
    return userRemoteService.updatePwd(userDto);
  }

  /**
   * 转换成 UserDto
   * @param userVo
   * @return
   */
  private UserDto convert(UserVo userVo){
    UserDto userDto = new UserDto();
    CommonUtils.copyProperties(userVo, userDto);
    userDto.setOperatorId(userVo.getSysUserId());
    userDto.setOperatorName(userVo.getSysRealName());
    return userDto;
  }
}
